<!--车辆分配管理页-->
<template>
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">车辆分配</span>
  </div>
  <!-- 申请单搜索卡片 -->
  <el-card style="margin:20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="工单申请人">
        <el-input placeholder="请输入申请人" style="width:220px;"
        v-model="searchUserForm.username" @keydown.enter.prevent="searchUser"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearch()">重置</el-button>
        <el-button type="primary" @click="searchUser">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>

  <!-- 工单信息列表 -->
  <el-card  style="margin:20px;">
    <el-radio-group style="margin-bottom: 15px;" v-model="type">
      <el-radio-button value="50" size="large" @change="searchUser">待分配</el-radio-button>
      <el-radio-button value="60" size="large" @change="searchUser">已分配</el-radio-button>
    </el-radio-group>
    <el-table :data="userDate">
      <el-table-column label="编号" type="index" width="100" align="center"></el-table-column>
      <el-table-column label="工单申请人" prop="username" align="center"></el-table-column>
      <el-table-column label="申请时间" prop="createTime" align="center"></el-table-column>
      <el-table-column label="出发地" prop="departureAddr" align="center"></el-table-column>
      <el-table-column label="目的地" prop="destinationAddr" align="center"></el-table-column>
      <el-table-column label="用车事由" prop="reason" align="center"></el-table-column>
      <el-table-column label="使用开始时间" prop="startTime" align="center"></el-table-column>
      <el-table-column label="使用结束时间" prop="endTime" align="center"></el-table-column>
      <el-table-column label="操作" width="100" align="center">
        <template #default="scope">
          <el-button type="primary" link v-if="type==50" @click="loadFence(scope.row.id)">分配用车</el-button>
          <el-button type="primary" link v-if="type==60" @click="back(scope.row)">还车</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>

  <!-- 围栏车辆概览弹窗 -->
  <el-dialog title="围栏列表" v-model="dialogVisible" style="padding:40px;"
  width="60%" max-width="1200px">
    <el-form>
      <el-row :gutter="10">
        <el-col :span="8">
          <el-form-item label="围栏名称:">
            <el-input placeholder="请输入" v-model="fenceSearchForm.name" @keydown.enter.prevent="loadFence"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="围栏状态">
            <el-select placeholder="请选择"v-model="fenceSearchForm.status" @change="loadFence">
              <el-option label="启用" value="1" />
              <el-option label="禁用" value="0"/>
            </el-select>
          </el-form-item>
        </el-col>
<el-col :span="4"></el-col>
        <el-col :span="4">
          <el-form-item>
<!--            下面这个代码的含义是使一组相关按钮在视觉上紧密相连（默认无间距或小间距）-->
            <el-button-group >
              <el-button @click="resetFenceSearch">重置</el-button>
              <el-button type="primary" @click="loadFence">查询</el-button>
            </el-button-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-table style=" width: 100%;margin:20px 0" :data="fenceDate">
      <el-table-column label="编号" type="index" width="80" align="center"></el-table-column>
      <el-table-column label="围栏名称" prop="name" width="200" align="center"></el-table-column>
      <el-table-column label="车辆总数" prop="totalNum" width="200" align="center"></el-table-column>
      <el-table-column label="可用车辆" prop="availableNum" align="center"></el-table-column>
      <el-table-column label="围栏状态" prop="status" align="center" :formatter="generalStausFomatter"></el-table-column>
      <el-table-column label="操作" width="100" align="center">
        <template #default="scope">
          <el-button type="primary" link @click="showVehicle(scope.row.id)">查看车辆</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>

  <!-- 分配车辆弹窗 -->
  <el-dialog title="分配车辆" style="padding:40px;" v-model="vehicleDialogVisible"
             width="60%" max-width="1200px">
    <el-table style="margin:20px 0" :data="vehicleDate">
      <el-table-column label="编号" type="index" width="80" align="center"></el-table-column>
      <el-table-column label="车辆品牌" prop="brand" width="200" align="center"></el-table-column>
      <el-table-column label="车牌号" prop="license" width="200" align="center"></el-table-column>
      <el-table-column label="车辆类型" prop="type" align="center" :formatter="vehicleTypeFomatter"></el-table-column>
      <el-table-column label="车辆状态" prop="status" align="center" :formatter="vehicleStatusFomatter"></el-table-column>
      <el-table-column label="操作" width="100" align="center">
        <template #default="scope">
          <el-button type="primary" link @click="distribute(scope.row.id)">分配</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
</template>

<script setup>
import {onMounted, ref} from "vue";
import qs from "qs";
import axios from "axios";
import {ElMessage} from "element-plus";
import index from "vuex";

// ***************************************************************************
// 这是搜索框的代码以及是加载页面的功能
// ***************************************************************************
const searchUserForm = ref({username:''});
// 将待分配作为默认值
const type = ref('50');
// 定义上一个数组来保存查询到的数据
const userDate = ref([]);

// 定义上一个方法，加载页面,搜索
const searchUser = () => {
  searchUserForm.value.status=type.value;
  let data = qs.stringify(searchUserForm.value);
  axios.get(BASE_URL+'v1/application/select?'+data)
      .then((reesponse)=>{
        if (reesponse.data.code==2000){
          userDate.value = reesponse.data.data;
        }else {
          ElMessage.error(reesponse.data.message);
        }
      })
}
// 这是重置的方法
const resetSearch =()=>{
  searchUserForm.value.username='';
  searchUser();
}
// 进入页面就进行加载
onMounted(()=>{
  searchUser();
})
// 这是还车的功能
// 现在是在申请表中，这个row是applicationVO
const back =(row) =>{
axios.post(BASE_URL+'v1/application/back/'+row.id+'/'+row.vehicleId)
    .then((response)=>{
      if (response.data.code==2000){
        ElMessage.success(response.data.message);
        searchUser();
      }else {
        ElMessage.error(response.data.message);
      }
    })
}
// *****************************************************************************************************
// 这是点击分配车辆按钮，弹出分配车辆弹窗的方法
// *****************************************************************************************************
const generalStatusArr =ref([]);
const vehicleTypeArr = ref([]);
const vehicleStatusArr = ref([]);
// 这是结合字典编辑围栏状态
onMounted(()=>{
  loadDictOption(generalStatusArr,'general_status');
  loadDictOption(vehicleTypeArr,'vehicle_type');
  loadDictOption(vehicleStatusArr,'vehicle_status');
})
const vehicleStatusFomatter = (row,column,cellValue,index)=>{
  if (!vehicleStatusArr.value){
    return cellValue;
  }
  for (let item of vehicleStatusArr.value){
    if (cellValue == item.value){
      cellValue = item.label;
    }
  }
  return cellValue;
}
const vehicleTypeFomatter = (row,column,cellValue,index)=>{
  if (!vehicleTypeArr.value){
    return cellValue;
  }
  for (let item of vehicleTypeArr.value){
    if (cellValue == item.value){
      cellValue = item.label;
    }
  }
  return cellValue;
}
const generalStausFomatter = (row,column,cellValue,index)=>{
  if (!generalStatusArr.value){
    return cellValue;
  }
  for (let item of generalStatusArr.value){
    if (cellValue == item.value){
      cellValue = item.label;
    }
  }
  return cellValue;
}
//用于控制围栏车辆概览弹窗是否可见
const dialogVisible = ref(false);
// 定义围栏的查询条件
const fenceSearchForm = ref({name:'',status:''});
// 定义数组来保存查询到的数据
const fenceDate = ref([]);
// 这里来定义上一个全局变量来保存申请表的id值
var applicationId;
// 定义加载围栏的方法
// 要注意这个id是applicationId
const loadFence = (id) => {
  applicationId = id;
  dialogVisible.value = true;
  let data = qs.stringify(fenceSearchForm.value);
  axios.get(BASE_URL+'v1/geofence/select?'+data)
      .then((reesponse)=>{
        if (reesponse.data.code==2000){
          fenceDate.value = reesponse.data.data;
          console.log(fenceDate.value)
        }else {
          ElMessage.error(reesponse.data.message);
        }
      })
}
// 定义重置搜索的方法
const resetFenceSearch = () => {
 fenceSearchForm.value.name='';
 fenceSearchForm.value.status='';
 loadFence();
}
// *********************************************************************************************************************
// 这是内层弹窗
// *********************************************************************************************************************
//用于控制分配车辆弹窗是否可见
const vehicleDialogVisible = ref(false);
// 这是用来保存查到的车辆的数组
const vehicleDate = ref([]);
// 这是点击查看车辆按钮，弹出内层弹窗的方法
const showVehicle = (id) => {
  vehicleDialogVisible.value = true;
  axios.get(BASE_URL+'v1/vehicle/select?geofenceId='+id)
      .then((reesponse)=>{
        if (reesponse.data.code==2000){
          vehicleDate.value = reesponse.data.data;
        }else {
          ElMessage.error(reesponse.data.message);
        }
      })
}
// 分配车辆
// 这个id值是车辆的id值
// 在这里面要有applicationId来修改vehicleId，传入了vehicleId修改车辆的状态
const distribute = (vehicleId) => {
axios.post(BASE_URL+'v1/application/distribute/'+vehicleId+'/'+applicationId)
    .then(((response)=>{
      if (response.data.code==2000){
        ElMessage.success('分配车辆成功!');
        vehicleDialogVisible.value = false;
        dialogVisible.value = false;
        searchUser();
      }else {
        ElMessage.error(response.data.message);
      }
    }))
}
</script>

<style scoped>

</style>